package com.woniu.book.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.ExceptionCode;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SnowflakeIdWorker;
import com.woniu.book.domain.Booking;
import com.woniu.book.dto.BookingDto;
import com.woniu.book.service.BookService;
import com.woniu.book.vo.BookingListVo;
import com.woniu.book.vo.BookingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/book")
public class BookController extends BaseController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    BookService bookService;

    @RequestMapping("/pk")
    public AjaxResult queryBookSn() {
        Long bookId = SnowflakeIdWorker.generateId();
        redisTemplate.opsForValue().set(bookId + "", bookId + "", 5, TimeUnit.MINUTES);
        return AjaxResult.success("ok", bookId +"");
    }

    @RequestMapping("/feign/getBookById")
    public Booking getBookById(@RequestParam("bookId") Long bookId){
       return bookService.queryBookByIdForFinance(bookId);

    }



    @RequestMapping("/feign/editBookingStatus")
    public Integer editBookingStatus(@RequestParam("bookingId") Long bookingId){
       return bookService.modifyBookingStatus(bookingId);
    }

    @RequestMapping("/makeBook")
    public AjaxResult makeBook(@RequestBody BookingDto booking) {

        //检查是否此床位已经被别人抢先占领
        checkBed(booking.getBedId());

        //检查订单id是否是伪造的,或者是否已经使用
        checkValidityOfBook(booking.getId(),booking.getBedId());

        //检查是否符合满减要求
        //生成订单
        bookService.makeBook(booking);
        return AjaxResult.success();
    }

    @GetMapping("/getBook")
    public AjaxResult getBook(Long bookingId){
       BookingVo bookingVo =  bookService.queryBookById(bookingId);
        //TODO 根据国籍id去查国籍

        //TODO 根据床位Id去查货币单位

       return AjaxResult.success("ok",bookingVo);
    }

    @RequestMapping("feign/getBookByIds")
    public List<Booking> getBookByIds(@RequestParam("ids") List<Long> ids){
      return   bookService.queryBookByIds(ids);
    }

    public void checkValidityOfBook(Long bookId,Integer bedId) {
        Boolean b = redisTemplate.delete(bookId + "");
        if (!b) {
            //若使用了无效的订单号,将此座位释放
            redisTemplate.delete(bedId+"");
            throw new CustomException(ExceptionCode.INVALID_ORDER_NO);
        }
    }

    public void checkBed(Integer bedId) {

        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(bedId + "", bedId + "",48,TimeUnit.HOURS);
        if (!aBoolean) {
            throw new CustomException(ExceptionCode.BED_ALREADY_SOLD);
        }
    }
    @RequestMapping("getBookByIds")
    public TableDataInfo getBooksById(HttpServletRequest request){
        startPage();
        String userId = request.getHeader("userId");
        List<BookingListVo> bookingListVos= bookService.getBooksById(Long.valueOf(userId));
        for (BookingListVo vo : bookingListVos) {
            vo.setBookingNo("p"+vo.getId());
        }
        return getDataTable(bookingListVos);
    }
    @RequestMapping("remove")
    public AjaxResult remove(Long id){
        bookService.remove(id);
        return AjaxResult.success();
    }

}
